import React, { useEffect, useState } from "react";

export default (): JSX.Element => {
  // 算法是寻找规律并实现
  const strFn = (str: string): string => {
    // 字符串反转
    // return str
    //   .split(" ")
    //   .map((item) => item.split("").reverse().join(""))
    //   .join(" ");
    // 字符串反转正则
    // return str
    //   .split(/\s/g)
    //   .map((item) => item.split("").reverse().join(""))
    //   .join(" ");
    return (str.match(/[\w']+/g) || []) // 对于let's这样的也要做判断
      .map((item) => item.split("").reverse().join(""))
      .join(" ");
  };
  const arrFn = (str: Number): any[] => {
    // 电话号码组合
    let btnCode = [
      " ",
      "1",
      "abc",
      "def",
      "ghk",
      "jkl",
      "mon",
      "pqrs",
      "tuv",
      "wxyz",
    ];
    let strCode: string[] = str
      .toString()
      .split("")
      .map((item) => btnCode[Number(item)]);
    let resC: string[] = [""];
    for (let i = 0; i <= strCode.length - 1; i++) {
      let res: string[] = [];
      resC.forEach((item) => {
        strCode[i].split("").forEach((items) => {
          res.push(`${item}${items}`);
        });
      });
      resC = res;
    }
    return resC;
  };
  const arrFn1 = (arr: any[]): any[] => {
    let res: any[] = [];
    let data: boolean = true;
    let min = Number.MAX_SAFE_INTEGER;
    arr.sort((a, b) => a - b);
    for (let i = 0, tmp = []; i <= arr.length - 1; i++) {
      if (tmp[0] !== arr[i] && tmp.length > 0) {
        // @ts-ignore
        res.push([].concat(tmp));
        min = min < tmp.length ? min : tmp.length;
        tmp.length = 0;
      }
      tmp.push(arr[i]);
      if (i === arr.length - 1) {
        // @ts-ignore
        res.push([].concat(tmp));
        min = min < tmp.length ? min : tmp.length;
      }
    }
    res.find((item) => {
      if (item.length % min !== 0 || item.length === 1) {
        data = false;
        return true;
      }
    });
    console.log(data);
    return res;
  };
  // 冒泡排序
  // 时间复杂度和空间复杂度最大为O(N^2)
  const demo1 = (arr: number[]) => {
    // 两次循环找出最大或最小值并交换位置
    for (let i = 0; i < arr.length; i++) {
      for (let j = i + 1; j < arr.length; j++) {
        if (arr[i] < arr[j]) {
          let tmp = arr[i];
          arr[i] = arr[j];
          arr[j] = tmp;
        }
      }
    }
    return arr
  };
  // 选择排序
  // 对比冒泡排序时间复杂度最大为O(N^2)但是空间复杂度最大为O(n)
  const demo2 = (arr: number[]) => {
    // 选出每个循环中的最小值和已经排序好的最大值进行对比如果最小值小于最大值则对换位置
    for (let i = 0; i < arr.length; i++) {
      let min = arr[i + 1]
      let minIndex = i + 1
      for (let j = i + 1; j < arr.length; j++) {
        if (arr[j] < min) {
          min = arr[j]
          minIndex = j
        }
      }
      if (arr[i] > min) {
        let tmp = arr[i]
        arr[i] = min
        arr[minIndex] = tmp
      }
    }
    return arr
  }
  // 插入排序
  // 取出目标项，将目标项的位置空出目标项和前面的对比，并将大于目标的项往后移动一位时间复杂度O(N^2)
  const demo3 = (arr: number[]) => {
    let index, current;
    for (let i = 1; i < arr.length; i++) {
      current = arr[i]
      index = i - 1
      while(index >= 0 && current < arr[index]) {
        arr[index + 1] = arr[index]
        index--
      }
      arr[index + 1] = current
    }
    return arr;
  }
  // 单调栈
  const demo4 = (arr: number[]) => {
    // 数组中当前位置后的下一个比当前位置大的位置之前的间隔
    // 相比暴力循环时间复杂度为O(n^2)和O(n)
    let res: any[] = []
    let stack: any[] = []
    for (let i = 0; i < arr.length; i++) {
      if (i === arr.length - 1) {
        res.push(0)
      }
      while(stack.length && arr[i] > arr[stack[stack.length - 1]]) {
        let index = stack.pop() || 0
        res[index] = i - index
      }
      if (stack.length && arr[i] < arr[stack[stack.length - 1]]) {
        res.push(0)
      }
      stack.push(i)
    }
    return res
  }

  useEffect(() => {
    // console.log(strFn("my name is hong"));
    // console.log(arrFn(13506258004));
    // console.log(arrFn1([1,2,3,4,5,1,2,3,4,5]))
    // console.log(demo1([2,3,3,5,3,2,4,5,7,8,9]))
    // console.log(demo2([2,3,3,5,3,2,4,5,7,8,9]))
    // console.log(demo3([2,1,3,6,4,5,7,9,8]))
    console.log(demo4([2,1,3,6,4,5,7,9,8]))
  }, []);
  return <div>demo</div>;
};


// 链表反转
// let head = {
//   value: 5,
//   next: {
//       value: 4,
//       next: {
//           value: 3,
//           next: {
//               value: 2,
//               next: {
//                   value: 1,
//                   next: null                
//               }
//           }
//       }    
//   }
// }

// var reverse = function (head) {
//   var pre = null
//   var next = null
//   var current = head
//   while (current !== null) {
//       next = current.next
//       current.next = pre
//       pre = current
//       current = next
//   }
//   return pre
// };
// reverse(head)

// 1 pre null current 54321
// 2 pre 5null current 4321
// 3 pre 45null current 321
// 4 pre 345null current 21
// 5 pre 2345null current 1
// 5 pre 12345null current null